﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using fmtx.Framework.DALFactory;
using fmtx.Framework.Img;
using SYH.IBLL;
using System.Configuration;

namespace SYH.Web.Controllers.Common
{
    public class FilesController : PubController
    {
        // GET: Files
        /// <summary>
        /// 预览文件
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult Index(string name,int w=1024,int h=500)
        {
            var path= Server.MapPath(name);
            if (System.IO.File.Exists(path))
            {
                var buff = ImageFactory.MakeThumbnail(path, w, h, ImageFactory.ImgModel.Width);
                var ext = System.IO.Path.GetExtension(name);
                ext = ext.Substring(1, ext.Length - 1);
                switch (ext)
                {
                    case "jpg":
                    case "gif":
                    case "png":
                    case "bmp":
                    case "jpge":
                        return Image(buff, ext);
                    default:
                        return Files(buff, ext);

                }
            }
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "No-Cache");
            Response.Cache.SetNoStore();
            return File(ImageFactory.MakeThumbnail(Server.MapPath("~/") + "Images/FileDefault.jpg", 490, 500, ImageFactory.ImgModel.Width), "image/jpg");
        }

        /// <summary>
        /// 图片类型
        /// </summary>
        /// <param name="file"></param>
        /// <param name="ext"></param>
        /// <returns></returns>
        private ActionResult Image(byte[] file, string ext)
        {
            return File(file, "image/" + ext);
        }

        /// <summary>
        /// 图片类型
        /// </summary>
        /// <param name="file"></param>
        /// <param name="ext"></param>
        /// <returns></returns>
        private ActionResult Image(byte[] file, string ext, string name)
        {
            return File(file, "image/" + ext, name);
        }

        /// <summary>
        /// 其他文件
        /// </summary>
        /// <param name="file"></param>
        /// <param name="ext"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private ActionResult Files(byte[] file, string ext)
        {
            return File(file, "application/" + ext);
        }

        /// <summary>
        /// 其他文件
        /// </summary>
        /// <param name="file"></param>
        /// <param name="ext"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private ActionResult Files(byte[] file, string ext, string name)
        {
            return File(file, "application/" + ext, name);
        }
    }
}